<%@page import="coredb.Validation"%>
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.persistence.NoResultException"%>
<%@page import="coredb.Queries" %>
<%@page import="java.util.Date" %>
<%@page import="java.util.List" %>
<%@page import="coredb.Account" %>
<%@page import="coredb.Transaction" %>
<%@page import="coredb.CustomerExistsException" %>
<%@page import="javax.persistence.NonUniqueResultException" %>
<%@page import="java.text.NumberFormat" %>
<%@page import="java.text.DecimalFormat" %>
<%@page import="java.text.SimpleDateFormat" %>
<%@page import="java.text.ParseException" %>
<%@page import="org.apache.commons.lang3.StringEscapeUtils" %>
<%@page errorPage="sorry.html" %>

<link rel="shortcut icon" href="favicon.ico" />
<title>Welcome to ABC Bank</title>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.23.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.23.custom.min.js"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function(){
        // Tabs
        $('#tabs').tabs();       
        // Datepicker
        $('.datepicker').datepicker({
            inline: true,
            changeMonth: true,
            changeYear: true,
            yearRange:"-90:+0"
        });
        
        $( "#dialog-message" ).dialog({
            modal: true,
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                }
            },
            width: 600
        });
    });
</script>
<style type="text/css">
    #tabs .ui-tabs-panel {
        height: 250px;
        overflow: auto;
    }
    .heading {
	color: #CCCCCC;
	alignment-adjust: cental alignment-adjust;
	font-size:42px;
	background-color:#03F;
	border-bottom-left-radius:20px;
	border-bottom-right-radius:20px;
	border-top-left-radius:20px;
	border-top-right-radius:20px;
	height:80px;
	padding:25px;
	padding-top:40px;
	margin:15px;
	font-family: Verdana, Geneva, sans-serif;
    }
    body{ 
	font:Georgia, "Times New Roman", Times, serif;
	font-size:11px;
	}
    .demoHeaders { margin-top: 2em; }
    #dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
    #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
    ul#icons {margin: 0; padding: 0;}
    ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
    ul#icons span.ui-icon {float: left; margin: 0 4px;}
    .formLabel {
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        font-weight: normal;
    }
    .formBox {
        font-family: Verdana, Geneva, sans-serif;
        font-size: 18px;
        color: #333;
    }
</style>


<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
<body>
<div class="heading"> <Center alignment &> ANVESHAK BANKING CORPORATION</div>    
<div id="tabs" style="width:90%;margin:5%;margin-top:2%;">
    <ul>
        <li><a href="#tabs-0">Home</a></li>
        <li><a href="#tabs-1">Create Account</a></li>
        <li><a href="#tabs-2">Deposit Money</a></li>
        <li><a href="#tabs-3">Withdraw Money</a></li>
        <li><a href="#tabs-4">Get Balance</a></li>
        <li><a href="#tabs-5">Daily Report</a></li>
        <li><a href="#tabs-8">Transaction History</a></li>
        <li><a href="#tabs-7">Help</a></li>
    </ul>
    <div id="tabs-0">
        <p style="font-size:14px">Welcome to Anveshak Banking Corporation.</p>
        <b>Internal Circulars:</b><br>
            <a href="#">New Home loan scheme</a><BR>
            <a href="#">Change in base rate effective 1 October 2012</a><BR>
            <a href="#">Bank timings change</a><BR>
       
    </div>
    <div id="tabs-1">
        <form method="POST">
            <table cellpadding="5" cellspacing="5">
                <tr><td class="formLabel">Enter Name: </td><td><span id="sprytextfield1">
                <input name="cusname" type="text" size="30">
              <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span></span></td></tr>
                <tr><td class="formLabel">Date of Birth:</td><td><span id="sprytextfield4">
                <input name="dob" type="text" class="datepicker" id="d1" value="1990-08-05" size="30">
              <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td></tr>
                <tr><td class="formLabel">PAN: </td><td><span id="sprytextfield2">
                <input name="pan" type="text" size="30" maxlength="10">
              <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span><span class="textfieldMaxCharsMsg">Exceeded maximum number of characters.</span></span></td></tr>
                <tr><td class="formLabel">Initial balance </td><td><span id="sprytextfield3">
                <input name="bal" type="text" value="500" size="30" maxlength="10">
              <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinValueMsg">The entered value is less than the minimum required.</span></span></td></tr>
                <tr><td><input type="submit" value="Create Account"></td><tr>
            </table>
        </form>
    </div>
    <div id="tabs-2">
        <form method="POST" action="index.jsp">
            <table cellpadding="5" cellspacing="5">
                <tr>
                    <td class="formLabel">Enter Account No:</td>
                    <td><span id="sprytextfield5">
                    <input type="text" name="depaccno">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
                </tr>
                <tr>
                    <td class="formLabel">Enter Amount to deposit:</td>
                    <td><span id="sprytextfield6">
                    <input type="text" name="amount">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinValueMsg">The entered value is less than the minimum required.</span></span></td>
                </tr>
                <tr>
                    <td class="formLabel">PAN:</td>
                    <td>
                        <input type="text" name="pan" maxlength="10">
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="Deposit"></td>
                </tr>
            </table>
        </form>

    </div>
    <div id="tabs-3">
        <form action="" method="post">
            <table  border="0" cellspacing="5" cellpadding="5">
                <tr>
                    <td class="formLabel">Enter Account No: </td>
                    <td><span id="sprytextfield8">
                    <input name="withdrawaccno" type="text">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span></span></td>
                </tr>
                <tr>
                    <td class="formLabel">Enter amount to withdraw: </td>
                    <td><span id="sprytextfield9">
                            <input name="withdrawamt" type="text">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinValueMsg">The entered value is less than the minimum required.</span></span></td>
                </tr>
                <tr>
                    <td><input name="Withdraw" type="submit" value="Withdraw"></td>
                </tr>
            </table>

        </form>
    </div>
    <div id="tabs-4">
        <form method="POST">
            <p class="formLabel">Enter account number:
                <span id="sprytextfield10">
                <input type="text" name="account">
                <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span></span>
<input type="submit" value="Show balance">
            </p>
        </form>
    </div>
    <div id="tabs-5">
        <form method="POST">
            <p class="formLabel">
                <input type="hidden" name="dlaccount" value="show">
                <input type="submit" value="Get today's transaction report">
            </p>
        </form>
    </div>
    
    <div id="tabs-7">
        Blah, blah blah...........ScrumMaster please fill this :)
    </div>
    <div id="tabs-8">
        <form method="POST">
            <table cellspacing="5" cellpadding="5">
                <tr>
                    <td>Enter Account no: </td>
                    <td><span id="sprytextfield11">
                    <input type="text" name="tsaccount">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span></span></td>
                </tr>
                <tr>
                    <td>Enter start date: </td>
                    <td><span id="sprytextfield12">
                    <input type="text" name="stdate" class="datepicker" id="d2">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
                </tr>
                <tr>
                    <td>Enter end date: </td>
                    <td><span id="sprytextfield13">
                    <input type="text" name="eddate" class="datepicker" id="d3">
                    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Show transaction history"></td>
                </tr>

            </table>

        </form>
        <script>
            $(function() {		
                $('.datepicker').each(function(){
                    $(this).datepicker( "option", "dateFormat", "yy-mm-dd");
                });
            
            });
        </script>
    </div>  

    <%-- 
  Tranaction History
    --%>
    <%
        if (request.getParameter("tsaccount") != null) {
            long account = 0;
            double amount = 0.0;
            Queries q = new Queries();
            out.println("<div id=\"dialog-message\" title=\"Transaction History\">");
            
            try {
                account = Long.parseLong(request.getParameter("tsaccount"));
                String sd = request.getParameter("stdate");
                String ed = request.getParameter("eddate");
                SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
                Date sdD = d.parse(sd);
                Date edD = d.parse(ed);
                if (sdD.after(edD)) {
                    out.println("Start date cannot be after end date.");
                    return;
                }
                List<Transaction> res = q.getTxnReport(account, sd, ed);
                out.println("<table cellpadding='6' cellspacing='6'>");
                out.println("<tr style='font-weight:bold'>");
                out.println("<td>Transaction ID</td><td>Time of Transaction</td><td>Amount</td><td>Type of Transaction</td>");
                out.println("</tr>");
                for (Transaction t : res) {
                    String typ = t.getTypeoftxn();
                    String typeOfTxn = "DEPOSIT";
                    if (typ.equals("W")) {
                        typeOfTxn = "WITHDRAWL";
                    }
                    out.println("<tr>");
                    out.println("<td>" + t.getTxnid() + "</td><td>" + String.valueOf(t.getTimeoftxn()) + "</td><td>" + String.valueOf(t.getAmount()) + "</td><td>" + typeOfTxn + "</td>");
                    out.println("</tr>");
                }
                out.println("</table>");
            } catch (ParseException p) {
                out.println("Illegal input(s). Please check and try again");
            } catch (NumberFormatException nfe) { 
                out.println("Improper account number. Please check.");
            } catch (Exception p) {
                out.println("No such account exists.");
            }  finally {
                out.println("</div>");
            }
        }
    %> 

    <%-- 
  Withdraw
    --%>
    <%

        if (request.getParameter("withdrawaccno") != null) {
            long account = 0;
            double amount = 0;
            out.println("<div id=\"dialog-message\" title=\"Withdraw Status\">");
            try {
                account = Long.parseLong(request.getParameter("withdrawaccno"));
                amount = Double.parseDouble(request.getParameter("withdrawamt"));
                Queries q = new Queries();
                if (amount <= 0) {
                    out.println("Improper amount entered. Please check and try again");
                    return;
                }
                boolean status = q.withdraw(account, amount);
                if (status) {
                    NumberFormat formatter = new DecimalFormat("#0.00");
                    out.println("Amount " + String.valueOf(amount) + " withdrawn sucessfully. Remaining balance is " + String.valueOf(formatter.format(q.getBal(account))));
                } else {
                    out.println("Amount could not be withdrawn.Set limits crossed. Check Transaction history.");
                }

            } catch (NumberFormatException n) {
                out.println("Illegal account number or amount");

            } catch (NoResultException nre) {
                out.println("No such account exists. Please check account number");
            } finally {
                out.println("</div>");
            }
        }
    %>

    <%-- 
   Create Account
    --%>
    <%
        if (request.getParameter("cusname") != null) {
            String name = StringEscapeUtils.escapeHtml4(request.getParameter("cusname"));
            
            String pan = StringEscapeUtils.escapeHtml4(request.getParameter("pan").toUpperCase());
            
            String dob = request.getParameter("dob") + " 00:00:00.000";
            out.println("<div id=\"dialog-message\" title=\"Account creation Status\">");
            
            try {
                 if (!Validation.validPan(pan)) {
                out.println("Account could not be created. PAN is in wrong format.");
                return;
                  }
                double amt = Double.parseDouble(request.getParameter("bal"));
                if (amt < 0) {
                    out.println("Account could not be created. Intitial balance amount cannot be negative");
                    return;
                }
                Queries q = new Queries();
                long accno;
                try {
                    accno = q.createAccount(name, dob, pan, amt);
                } catch(CustomerExistsException cee) {
                    out.println("Customer already exists. Account could not be created");
                    return;
                }
                if (accno != Queries.ACCOUNT_NOT_CREATED) {
                    out.println("Account created: Account number is " + String.valueOf(accno));
                } else {
                    out.println("Account could not be created. Please check and try again.");
                }
            } catch (NonUniqueResultException pa) {
                out.println("Customer already exists. Account could not be created.");
            } finally {
                out.println("</div>");
            }
        }
    %>

    <%-- 
   Deposit
    --%>
    <%
        if (request.getParameter("depaccno") != null) {
            long account = 0;
            double amt = 0.0;
            String pan = request.getParameter("pan").toUpperCase();
            out.println("<div id=\"dialog-message\" title=\"Deposit Status\">");
            try {
                account = Long.parseLong(request.getParameter("depaccno"));
                amt = Double.parseDouble(request.getParameter("amount"));
                Queries q = new Queries();
                if (amt <= 0) {
                    out.println("Improper amount entered. Please check and try again");
                    return;
                }
                boolean status;
                if (amt > Queries.PAN_REQUIRED_LT) {
                    status = q.deposit(account, amt, pan);
                } else {
                    status = q.deposit(account, amt);
                }
                if (status) {
                    out.println("Amount of " + String.valueOf(amt) + " deposited in " + String.valueOf(account) + ". Balance is " + String.valueOf(q.getBal(account)));
                } else {
                    out.println("Amount could not be deposited. Check PAN if amount is greater than " + String.valueOf(Queries.PAN_REQUIRED_LT));
                }
            } catch (NoResultException nre) {
                out.println("No such account exists. Please check account number");
            } catch (NumberFormatException n) {
                out.println("Improper Account number or amount. Please check");
            } finally {
                out.println("</div>");
            }
        }
    %>

    <%-- 
    Show Balance
    --%>
    <%

        if (request.getParameter("account") != null) {
            long account = 0;
            out.println("<div id=\"dialog-message\" title=\"Show balance\">");
            try {
                account = Long.parseLong(request.getParameter("account"));
                Queries q = new Queries();
                double bal = q.getBal(account);
                if (bal != Queries.ACCOUNT_NOT_FOUND) {
                    out.println("Account number " + String.valueOf(account) + " has balance " + String.valueOf(bal));
                } else {
                    out.println("Invalid account number. Check and try again");
                }
            } catch (NumberFormatException n) {
                out.println("Invalid account number. Check and try again");
            } finally {
                out.println("</div>");
            }
        }
    %>

    <%-- 
    Today's Transaction Report
    --%>
    <%
        if (request.getParameter("dlaccount") != null) {
            long account = 0;
            out.println("<div id=\"dialog-message\" title=\"Today's Transaction Report\">");
            out.println("<table cellpadding='6' cellspacing='6'>");
            out.println("<tr style='font-weight:bold'>");
            out.println("<td>Account No</td><td>Transaction ID</td><td>Time of Transaction</td><td>Amount</td><td>Type of Transaction</td>");
            out.println("</tr>");
            try {
                Queries q = new Queries();
                List<Transaction> res = q.getDailyReport();
                for (Transaction t : res) {
                    String typ = t.getTypeoftxn();
                    String typeOfTxn = "DEPOSIT";
                    if (typ.equals("W")) {
                        typeOfTxn = "WITHDRAWL";
                    }
                    out.println("<tr>");
                    out.println("<td>" + String.valueOf(t.getAccno().getAccno()) + "</td><td>" + t.getTxnid() + "</td><td>" + String.valueOf(t.getTimeoftxn()) + "</td><td>" + String.valueOf(t.getAmount()) + "</td><td>" + String.valueOf(typeOfTxn) + "</td>");
                    out.println("</tr>");
                }
                out.println("</table>");
                out.println("<a href='download.jsp'>Export as CSV (can be opened with MS Excel)</a>");
            } catch (Exception n) {
                out.println("An erro occured while fetching results. Please try again");
            } finally {
                out.println("</div>");
            }
        }
    %>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none", {validateOn:["change"], minChars:2});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "none", {minChars:10, maxChars:10, validateOn:["change"]});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "currency", {validateOn:["change"], minValue:0});
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "date", {format:"yyyy-mm-dd", validateOn:["change"]});
var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5", "integer", {validateOn:["change"], minChars:6});
var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6", "real", {validateOn:["change"], minValue:0});
var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7", "none", {validateOn:["change"]});
var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8", "integer", {minChars:6, validateOn:["change"]});
var sprytextfield9 = new Spry.Widget.ValidationTextField("sprytextfield9", "real", {minValue:0, validateOn:["change"]});
var sprytextfield10 = new Spry.Widget.ValidationTextField("sprytextfield10", "integer", {minChars:6, validateOn:["change"]});
var sprytextfield11 = new Spry.Widget.ValidationTextField("sprytextfield11", "integer", {validateOn:["change"], minChars:6});
var sprytextfield12 = new Spry.Widget.ValidationTextField("sprytextfield12", "date", {format:"yyyy-mm-dd", validateOn:["change"]});
var sprytextfield13 = new Spry.Widget.ValidationTextField("sprytextfield13", "date", {format:"yyyy-mm-dd", validateOn:["change"]});
</script>
</div>
</body>